package info.liuxing.okhttp.proxy;

import java.io.IOException;
import java.net.*;
import java.util.Arrays;
import java.util.List;

/**
 * 自定义代理选择器
 *
 * @author liuxing (SE)
 * @summary 自定义代理选择器
 * @Copyright (c) 2017, Lianjia Group All Rights Reserved.
 * @since 2017-12-01 00:43
 */
public class CustomProxySelector extends ProxySelector {

    ProxySelector proxySelector = ProxySelector.getDefault();

    List<Proxy> proxies = Arrays.asList(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 11087)));

    @Override
    public List<Proxy> select(URI uri) {
        if (uri.getHost().contains("google.com")) {
            return proxies;
        }

        return proxySelector.select(uri);
    }

    @Override
    public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
        proxySelector.connectFailed(uri, sa, ioe);
    }
}
